package com.atguigu.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class ThreadTest {
    public static Executor executor= Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println(Thread.currentThread().getId()+"--------main thread start..........");
//        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            int b = 10 / 0;
//            System.out.println(Thread.currentThread().getId()+"-----------result --------------" + b);
//            return b;
//        }, executor).whenCompleteAsync((res,exception)->{
//            System.out.println(Thread.currentThread().getId()+"-----------异步结果执行借宿 --------------" +res);
//        }).exceptionally(throwable -> {
//            System.out.println(Thread.currentThread().getId()+"-----------异步结果执行异常 --------------");
//            return 10;
//        });
//        Integer integer = integerCompletableFuture.get();
//        System.out.println(Thread.currentThread().getId()+"-----future result ............"+integer);

//        CompletableFuture.runAsync(()->{
//            int a = 2;
//            int b = 10 / a;
//            System.out.println(Thread.currentThread().getId()+"-----------result --------------" + b);
//        },executor);

//        CompletableFuture<Integer> handle = CompletableFuture.supplyAsync(() -> {
//            int a = 10 / 2;
//            return a;
//        }, executor).handle((res, exception) -> {
//            if (res != null) {
//                return res;
//            } else if (exception != null) {
//                return 1;
//            }
//            return 0;
//        });
//        System.out.println(Thread.currentThread().getId()+"-----future result ............"+handle.get());

//        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            int a = 10 / 2;
//            System.out.println(Thread.currentThread().getId()+"is run..........");
//            return a;
//        }, executor).thenApplyAsync((res) -> {
//            System.out.println(Thread.currentThread().getId()+"is run.........."+res);
//            return "helle" + res;
//        }, executor);
//        System.out.println(Thread.currentThread().getId()+"-----future result ............"+stringCompletableFuture.get());

        CompletableFuture<Long> futrue1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getId()+"线程启动");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getId()+"线程结束");
            return Thread.currentThread().getId();
        }, executor);

        CompletableFuture<Long> futrue2 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getId()+"线程启动");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getId()+"线程结束");
            return Thread.currentThread().getId();
        }, executor);


//        futrue1.runAfterBothAsync(futrue2,()->{
//            System.out.println(Thread.currentThread().getId()+"线程启动");
//        });

//        futrue1.thenAcceptBoth(futrue2,(res1,res2)->{
//            System.out.println(Thread.currentThread().getId()+"线程启动---last result----"+res1+","+res2);
//        });

//        CompletableFuture<String> future3 = futrue1.thenCombine(futrue2, (res1, res2) -> {
//            System.out.println(Thread.currentThread().getId()+"线程启动---last result----"+res1+","+res2);
//            return res1 +","+ res2;
//        });



//        futrue1.applyToEitherAsync(futrue2,(res)->{
//            System.out.println(Thread.currentThread().getId()+"线程启动---last result----"+res);
//            return "hello:"+res;
//        },executor);


        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getId()+"futureImg线程启动");
            return "查询图片信息";
        }, executor);

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getId()+"futureAttr线程启动");
            return "查询属性信息";
        }, executor);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getId()+"futureDesc线程启动");
            return "查询商品介绍";
        }, executor);

//        futureImg.get();
//        futureAttr.get();
//        futureDesc.get();
//        CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
        CompletableFuture<Object> future = CompletableFuture.anyOf(futureAttr, futureImg, futureDesc);

        System.out.println(Thread.currentThread().getId()+"--------main thread end............"+future.get());
    }
}
